home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C & C++ Multimedia Cyber Classroom
/
C and C++ Multimedia Cyber Classroom (Prentice Hall) (1998).iso
/
cpphtp2
/
code.jar
/
code
/
ch09
/
fig09_07.txt
< prev
next >
Wrap
Text File
|
1998-02-27
|
2KB
|
102 lines
1 // Fig. 9.7: point2.h
2 // Definition of class Point
3 #ifndef POINT2_H
4 #define POINT2_H
5
6 class Point {
7 public:
8 Point( int = 0, int = 0 ); // default constructor
9 ~Point(); // destructor
10 protected: // accessible by derived classes
11 int x, y; // x and y coordinates of Point
12 };
13
14 #endif
15
16
17 // Fig. 9.7: point2.cpp
18 // Member function definitions for class Point
19 #include <iostream.h>
20 #include "point2.h"
21
22 // Constructor for class Point
23 Point::Point( int a, int b )
24 {
25 x = a;
26 y = b;
27
28 cout << "Point constructor: "
29 << '[' << x << ", " << y << ']' << endl;
30 }
31
32 // Destructor for class Point
33 Point::~Point()
34 {
35 cout << "Point destructor: "
36 << '[' << x << ", " << y << ']' << endl;
37 }
38
39
40 // Fig. 9.7: circle2.h
41 // Definition of class Circle
42 #ifndef CIRCLE2_H
43 #define CIRCLE2_H
44
45 #include "point2.h"
46
47 class Circle : public Point {
48 public:
49 // default constructor
50 Circle( double r = 0.0, int x = 0, int y = 0 );
51
52 ~Circle();
53 private:
54 double radius;
55 };
56
57 #endif
58
59
60 // Fig. 9.7: circle2.cpp
61 // Member function definitions for class Circle
62 #include "circle2.h"
63
64 // Constructor for Circle calls constructor for Point
65 Circle::Circle( double r, int a, int b )
66 : Point( a, b ) // call base-class constructor
67 {
68 radius = r; // should validate
69 cout << "Circle constructor: radius is "
70 << radius << " [" << x << ", " << y << ']' << endl;
71 }
72
73 // Destructor for class Circle
74 Circle::~Circle()
75 {
76 cout << "Circle destructor: radius is "
77 << radius << " [" << x << ", " << y << ']' << endl;
78 }
79
80
81 // Fig. 9.7: fig09_07.cpp
82 // Demonstrate when base-class and derived-class
83 // constructors and destructors are called.
84 #include <iostream.h>
85 #include "point2.h"
86 #include "circle2.h"
87
88 int main()
89 {
90 // Show constructor and destructor calls for Point
91 {
92 Point p( 11, 22 );
93 }
94
95 cout << endl;
96 Circle circle1( 4.5, 72, 29 );
97 cout << endl;
98 Circle circle2( 10, 5, 5 );
99 cout << endl;
100 return 0;
101 }